home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / El temporizador y la hora / AnalogClock / ClockControl.cs < prev   
Encoding:
Text File  |  2001-01-15  |  4.1 KB  |  133 lines

  1. //-------------------------------------------
  2. // ClockControl.cs ⌐ 2001 by Charles Petzold
  3. //-------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Windows.Forms;
  8.  
  9. namespace Petzold.ProgrammingWindowsWithCSharp
  10. {
  11. class ClockControl: UserControl
  12. {
  13.      DateTime dt;
  14.  
  15.      public ClockControl()
  16.      {
  17.           ResizeRedraw = true;
  18.           Enabled = false;
  19.      }
  20.      public DateTime Time
  21.      {
  22.           get 
  23.           { 
  24.                return dt;  
  25.           }
  26.           set 
  27.           { 
  28.                Graphics grfx = CreateGraphics();
  29.                InitializeCoordinates(grfx);
  30.  
  31.                Pen pen = new Pen(BackColor);
  32.  
  33.                if (dt.Hour != value.Hour)
  34.                {
  35.                     DrawHourHand(grfx, pen);
  36.                }
  37.                if (dt.Minute != value.Minute)
  38.                {
  39.                     DrawHourHand(grfx, pen);
  40.                     DrawMinuteHand(grfx, pen);
  41.                }
  42.                if (dt.Second != value.Second)
  43.                {
  44.                     DrawMinuteHand(grfx, pen);
  45.                     DrawSecondHand(grfx, pen);
  46.                }
  47.                if (dt.Millisecond != value.Millisecond)
  48.                {
  49.                     DrawSecondHand(grfx, pen);
  50.                }          
  51.                dt  = value;
  52.                pen = new Pen(ForeColor);
  53.  
  54.                DrawHourHand(grfx, pen);
  55.                DrawMinuteHand(grfx, pen);
  56.                DrawSecondHand(grfx, pen);
  57.  
  58.                grfx.Dispose();
  59.           }
  60.      }
  61.      protected override void OnPaint(PaintEventArgs pea)
  62.      {
  63.           Graphics grfx  = pea.Graphics;
  64.           Pen      pen   = new Pen(ForeColor);
  65.           Brush    brush = new SolidBrush(ForeColor);
  66.  
  67.           InitializeCoordinates(grfx);
  68.           DrawDots(grfx, brush);
  69.           DrawHourHand(grfx, pen);
  70.           DrawMinuteHand(grfx, pen);
  71.           DrawSecondHand(grfx, pen);
  72.      }
  73.      void InitializeCoordinates(Graphics grfx)
  74.      {
  75.           if (Width == 0 || Height == 0)
  76.                return;
  77.  
  78.           grfx.TranslateTransform(Width / 2, Height / 2);
  79.  
  80.           float fInches = Math.Min(Width / grfx.DpiX, Height / grfx.DpiY);
  81.  
  82.           grfx.ScaleTransform(fInches * grfx.DpiX / 2000,
  83.                               fInches * grfx.DpiY / 2000);
  84.      }
  85.      void DrawDots(Graphics grfx, Brush brush)
  86.      {
  87.           for (int i = 0; i < 60; i++)
  88.           {
  89.                int iSize = i % 5 == 0 ? 100 : 30;
  90.  
  91.                grfx.FillEllipse(brush, 0 - iSize / 2, -900 - iSize / 2, 
  92.                                        iSize, iSize);
  93.                grfx.RotateTransform(6);
  94.           }          
  95.      }
  96.      protected virtual void DrawHourHand(Graphics grfx, Pen pen)
  97.      {
  98.           GraphicsState gs = grfx.Save();
  99.           grfx.RotateTransform(360f * Time.Hour / 12 +
  100.                                 30f * Time.Minute / 60);
  101.  
  102.           grfx.DrawPolygon(pen, new Point[]
  103.                               {
  104.                                    new Point(0,  150), new Point( 100, 0),
  105.                                    new Point(0, -600), new Point(-100, 0)
  106.                               });
  107.           grfx.Restore(gs);
  108.      }
  109.      protected virtual void DrawMinuteHand(Graphics grfx, Pen pen)
  110.      {
  111.           GraphicsState gs = grfx.Save();
  112.           grfx.RotateTransform(360f * Time.Minute / 60 +
  113.                                  6f * Time.Second / 60);
  114.  
  115.           grfx.DrawPolygon(pen, new Point[]
  116.                               {
  117.                                    new Point(0,  200), new Point( 50, 0),
  118.                                    new Point(0, -800), new Point(-50, 0)
  119.                               });
  120.           grfx.Restore(gs);
  121.      }
  122.      protected virtual void DrawSecondHand(Graphics grfx, Pen pen)
  123.      {
  124.           GraphicsState gs = grfx.Save();
  125.           grfx.RotateTransform(360f * Time.Second / 60 +
  126.                                  6f * Time.Millisecond / 1000);
  127.  
  128.           grfx.DrawLine(pen, 0, 0, 0, -800);
  129.           grfx.Restore(gs);          
  130.      }
  131. }
  132. }
  133.